#
# @lc app=leetcode.cn id=513 lang=python3
#
# [513] 找树左下角的值
#
import collections
from typing import Optional

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
# @lc code=start
# Definition for a binary tree node.

class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return None
        queue = collections.deque()
        queue.append(root)
        ans = root.val
        while queue:
            size = len(queue)
            ans = queue[0].val
            while size > 0:
                size -= 1
                node = queue.popleft()
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return ans

# @lc code=end

